<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="标题" prop="feedBackTitle">
        <el-input
          v-model="queryParams.feedBackTitle"
          placeholder="请输入反馈标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="readStatus">
        <el-select v-model="queryParams.readStatus" placeholder="请选择状态" clearable size="small">
          <el-option label="全部" value />
          <el-option label="已处理" :value="1" />
          <el-option label="未处理" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="feedBackType">
        <el-select v-model="queryParams.feedBackType" placeholder="请选择反馈类型" clearable size="small">
          <el-option label="意见反馈" :value="1" />
          <el-option label="顾问咨询" :value="2" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="feedBackTitle" />
      <el-table-column label="类型" align="center" prop="feedBackType">
        <template slot-scope="scope">
          <span>{{ scope.row.feedBackType | filterFeedBackType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column label="手机/邮箱/QQ" align="center" prop="feedBackChannel" />
      <el-table-column label="时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="readStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.readStatus | filterReadStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:feedback:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listFeedback,
  getFeedback,
  delFeedback,
  exportFeedback
} from "@/api/system/feedback";

export default {
  name: "Feedback",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      feedBackTitles: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 反馈表格数据
      feedbackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        readStatus: "",
        pageNum: 1,
        pageSize: 10,
        feedBackTitle: null,
        feedBackType: null,
        feedBackChannel: null,
        userId: null,
        feedBackImgs: null
      }
    };
  },
  filters: {
    filterFeedBackType(value) {
      return {
        1: "意见反馈",
        2: "顾问咨询"
      }[value];
    },
    filterReadStatus(value) {
      return {
        1: "已处理",
        2: "未处理"
      }[value];
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询反馈列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.feedBackTitles = selection.map(item => item.feedBackTitle);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 查看按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "FeedbackDetail",
        query: {
          id: row.id
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const feedBackTitles = row.feedBackTitle || this.feedBackTitles;

      this.$confirm(
        '是否确认删除标题为"' + feedBackTitles + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delFeedback(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportFeedback(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
